<html>
<head>

<title>Groovy Goodness: Use Connection Parameters to Get Text From URL</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Use Connection Parameters to Get Text From URL</h3>

<div class="post">
<p>For a long time we can simply <a href="http://mrhaki.blogspot.com/2009/10/groovy-goodness-reading-url-content.html">get the text from an URL</a> in Groovy. Since Groovy 1.8.1 we can set parameters to the underlying <code>URLConnection</code> that is used to get the content. The parameters are passed as a Map to the <code>getText()</code> method or to the <code>newReader()</code> or <code>newInputStream()</code> methods for an URL.</p><p>We can set the following parameters:<br />
<ul><li>connectTimeout in milliseconds</li>
<li>readTimeout in milliseconds</li>
<li>useCaches</li>
<li>allowUserInteraction</li>
<li>requestProperties is a Map with general request properties</li>
</ul></p><pre class="brush:groovy">// Contents of http://www.mrhaki.com/url.html:
// Simple test document
// for testing URL extensions
// in Groovy.

def url = "http://www.mrhaki.com/url.html".toURL()

// Simple Integer enhancement to make
// 10.seconds be 10 * 1000 ms.
Integer.metaClass.getSeconds = { -&gt;
    delegate * 1000
}

// Get content of URL with parameters.
def content = url.getText(connectTimeout: 10.seconds, readTimeout: 10.seconds,
                          useCaches: true, allowUserInteraction: false,
                          requestProperties: ['User-Agent': 'Groovy Sample Script'])

assert content == '''\
Simple test document
for testing URL extensions
in Groovy.
'''

url.newReader(connectTimeout: 10.seconds, useCaches: true).withReader { reader -&gt;
    assert reader.readLine() == 'Simple test document'
}
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>